package fy.form.listen.dao;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgLevel;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.bo.V3xOrgRole;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.v3x.services.V3XLocator;

public class MyOrgManager {
	private static OrgManager orgManager;

	private static MyOrgManager myOrgManager;

	private Map<Long, Long> approverMap;

	private MyOrgManager() throws Exception {

		if (orgManager == null)
			orgManager = V3XLocator.getInstance().lookup(OrgManager.class);
		approverMap = new Hashtable<Long, Long>();
		List<V3xOrgAccount> accounts = getAllAccounts();
		if (accounts != null) {
			for (int i = 0, size = accounts.size(); i < size; i++) {
				V3xOrgAccount account = accounts.get(i);
				if (!"集团总办".equals(account.getName())) {
					for (Iterator<V3xOrgLevel> iterator = orgManager
							.getAllLevels(account.getId()).iterator(); iterator
							.hasNext();) {
						V3xOrgLevel level = iterator.next();
						if (level.getName().equals("总经理")) {
							addApprover(account.getId(), level.getId());
						}
					}
				} else {
					for (Iterator<V3xOrgLevel> iterator = orgManager
							.getAllLevels(account.getId()).iterator(); iterator
							.hasNext();) {
						V3xOrgLevel level = iterator.next();
						if (level.getName().equals("总裁")) {
							addApprover(account.getId(), level.getId());
						}
					}
				}
			}
		}
	}

	private void addApprover(long accountID, long levelID) {
		List<V3xOrgMember> members = getMemberByLevel(levelID);

		if (members != null && members.size() > 0) {
			this.approverMap.put(accountID, levelID);
		}

		members = null;
	}

	public static MyOrgManager getInstance() {
		if (myOrgManager == null) {
			synchronized (MyOrgManager.class) {
				if (myOrgManager == null)
					try {
						myOrgManager = new MyOrgManager();
					} catch (Exception e) {
						e.printStackTrace();
					}
			}
		}
		return myOrgManager;
	}

	private List<V3xOrgAccount> getAllAccounts() {
		try {
			return orgManager.getAllAccounts();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public V3xOrgMember getMemberByID(long userID) {
		try {
			return orgManager.getMemberById(userID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<V3xOrgMember> getDepManager(long accountID, long depID) {
		try {
			return orgManager.getMembersByDepartmentRole(depID, "DepManager");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public V3xOrgMember getMemberByLoginName(String loginName) {
		try {
			return orgManager.getMemberByLoginName(loginName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<V3xOrgPost> getAllPosts(long accountID) {
		try {
			return orgManager.getAllPosts(accountID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	public List<V3xOrgMember> getMemberByPostID(long postID) {
		try {
			return orgManager.getMembersByPost(postID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<V3xOrgMember> getMemberByLevel(long levelID) {
		try {
			return orgManager.getMembersByLevel(levelID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public V3xOrgDepartment getDepByID(long depID) {
		try {
			return orgManager.getDepartmentById(depID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public V3xOrgDepartment getParentDepByID(long depID) {
		try {
			return orgManager.getParentDepartment(depID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public V3xOrgRole getRole(long roleID) {
		try {
			return orgManager.getRoleById(roleID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public long getApproverLevelID(long accountID) {
		return this.approverMap.get(accountID);
	}
}
